//
//  SAIndexVC.m
//  YunFu
//
//  Created by Bager on 2018/9/10.
//  Copyright © 2018年 yunfu. All rights reserved.
//

#import "SAIndexVC.h"

#import "SAGoodsListVC.h"
#import "SAShopListVC.h"
#import "SAMarketListVC.h"
#import "CyclePagerView.h"
#import "HistorySearchVC.h"
#import "SAIndexCollectionView.h"
#import "SAIndexActivityView.h"
#import "IndexTabBarVC.h"

@interface SAIndexVC ()<YNPageViewControllerDelegate,YNPageViewControllerDataSource,UIScrollViewDelegate>

@property (nonatomic, strong) SAIndexCollectionView *indexCV;
@property (nonatomic, strong) SAIndexActivityView *activityView;
@property (nonatomic, strong) UIImageView *activityImg;

@end

@implementation SAIndexVC

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self createView];
    
    [self createHeader];
}

- (void)createView
{
    YNPageConfigration *configration = [YNPageConfigration defaultConfig];
    configration.pageStyle = YNPageStyleSuspensionTop;
    configration.headerViewCouldScale = YES;
    configration.showTabbar = YES;
    configration.suspenOffsetY = 64;
    configration.showNavigation = NO;
    configration.scrollMenu = NO;
    configration.aligmentModeCenter = NO;
    configration.lineWidthEqualFontWidth = YES;
    configration.showBottomLine = YES;
    configration.normalItemColor = [UIColor blackColor];
    configration.selectedItemColor = kPublicColorRed;
    configration.lineColor = kPublicColorRed;
    configration.bottomLineBgColor = [UIColor whiteColor];
    
    YNPageViewController *pageView = [YNPageViewController pageViewControllerWithControllers:[self getArrayVCs] titles:[self getArrayTitles] config:configration];
    pageView.delegate = self;
    pageView.dataSource = self;
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kPublicNavHeight + 190 + ((kScreenWidth - kPublicMargin * 2) / 5.0 + 20) * 2 + 200 + 180)];
    headerView.backgroundColor = kPublicColorLightGray;
    
    CyclePagerView *page = [[CyclePagerView alloc] initWithFrame:CGRectMake(0, kPublicNavHeight, kScreenWidth, 180) imgArray:@[@"",@"",@""]];
    [headerView addSubview:page];
    
    DefineWeakSelf;
    page.callBack = ^(id result) {
        
    };
    
    [headerView addSubview:self.indexCV];
    [headerView addSubview:self.activityView];
    [headerView addSubview:self.activityImg];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(80, CGRectGetMaxY(self.activityImg.frame) + 24, kScreenWidth - 80 * 2, 1)];
    line.backgroundColor = HexColor(0x0c97ec);
    [headerView addSubview:line];
    
    UILabel *lineText = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2.0 - 65, CGRectGetMaxY(self.activityImg.frame), 130, 50)];
    lineText.text = @"猜你喜欢";
    lineText.font = [UIFont systemFontOfSize:14];
    lineText.textAlignment = NSTextAlignmentCenter;
    lineText.backgroundColor = kPublicColorLightGray;
    lineText.textColor = HexColor(0x0c97ec);
    [headerView addSubview:lineText];

    pageView.headerView = headerView;
    
    [pageView addSelfToParentViewController:self];
}

- (void)createHeader
{
    UIView *headerBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenHeight, kPublicNavHeight)];
    headerBg.backgroundColor = kPublicColorMain;
    [self.view addSubview:headerBg];
    
    [self creatSearchNaviBar];
}

- (void)creatSearchNaviBar
{
    LLSearchNaviBarView *searchNaviBarView = [LLSearchNaviBarView new];
    searchNaviBarView.backgroundColor = kPublicColorRed;
    searchNaviBarView.searbarPlaceHolder = @"请输入搜索关键词";
    
    @LLWeakObj(self);
    [searchNaviBarView showbackBtnWith:[UIImage imageNamed:@"navi_back_w"] onClick:^(UIButton *btn) {
        @LLStrongObj(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [searchNaviBarView showbackBtnWith:[UIImage imageNamed:@"back"] onClick:^(UIButton *btn) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        IndexTabBarVC *index = [IndexTabBarVC new];
        window.rootViewController = index;
    }];
    
    [searchNaviBarView showRightOneBtnWith:[UIImage imageNamed:@"sandian"] onClick:^(UIButton *btn) {
        
    }];
    
    [searchNaviBarView setSearchBarBeignOnClickBlock:^{
        @LLStrongObj(self);
        
        HistorySearchVC *searShopVC = [HistorySearchVC new];
        
        
        @LLWeakObj(searShopVC);
        //(1)点击分类 (2)用户点击键盘"搜索"按钮  (3)点击历史搜索记录
        [searShopVC beginSearch:^(NaviBarSearchType searchType,NBSSearchShopCategoryViewCellP *categorytagP,UILabel *historyTagLabel,LLSearchBar *searchBar) {
            @LLStrongObj(searShopVC);
            
            NSLog(@"搜索结果：historyTagLabel:%@--->searchBar:%@--->categotyTitle:%@--->%@",historyTagLabel.text,searchBar.text,categorytagP.categotyTitle,categorytagP.categotyID);
            
            searShopVC.searchBarText = @"你选择的搜索内容显示到这里";
        }];
        
        //执行即时搜索匹配
        NSArray *tempArray = @[@"Java", @"Python"];
        
        [searShopVC searchbarDidChange:^(NaviBarSearchType searchType, LLSearchBar *searchBar, NSString *searchText) {
            @LLStrongObj(searShopVC);
            
            NSLog(@"搜索文字++++++%@", searchText);
            
            //FIXME:这里模拟网络请求数据!!!
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                searShopVC.resultListArray = tempArray;
            });
        }];
        
        [searShopVC resultListViewDidSelectedIndex:^(UITableView *tableView, NSInteger index) {
            NSLog(@"搜索结果++++++%@", tempArray[index]);
        }];
        
        [self presentViewController:searShopVC animated:nil completion:nil];
    }];
    
    [self.view addSubview:searchNaviBarView];
}

- (NSArray *)getArrayVCs
{
    SAGoodsListVC *goods = [SAGoodsListVC new];
    goods.appearCallBack = ^{
        
    };
    SAShopListVC *shops = [SAShopListVC new];
    shops.appearCallBack = ^{
        
    };
    SAMarketListVC *market = [SAMarketListVC new];
    market.appearCallBack = ^{
        
    };
    
    return @[goods, shops, market];
}

- (NSArray *)getArrayTitles
{
    return @[@"宝贝推荐", @"优选店铺", @"各大商场"];
}

- (UIScrollView *)pageViewController:(YNPageViewController *)pageViewController pageForIndex:(NSInteger)index
{
    if (index == 0)
    {
        SAGoodsListVC *vc = pageViewController.controllersM[index];
        return vc.table;
    }
    else if (index == 1)
    {
        SAShopListVC *vc = pageViewController.controllersM[index];
        return vc.table;
    }
    else
    {
        SAMarketListVC *vc = pageViewController.controllersM[index];
        return vc.table;
    }
}

- (SAIndexCollectionView *)indexCV
{
    if (!_indexCV)
    {
        _indexCV = [[SAIndexCollectionView alloc] init];
    }
    return _indexCV;
}

- (SAIndexActivityView *)activityView
{
    if (!_activityView)
    {
        _activityView = [[SAIndexActivityView alloc] initWithFrame:CGRectMake(0, kPublicNavHeight + 190 + ((kScreenWidth - kPublicMargin * 2) / 5.0 + 20) * 2, kScreenWidth, 200)];
    }
    return _activityView;
}

- (UIImageView *)activityImg
{
    if (!_activityImg)
    {
        _activityImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.activityView.frame) + 10, kScreenWidth, 120)];
        _activityImg.backgroundColor = [UIColor cyanColor];
        [_activityImg setImage:[UIImage imageNamed:@"yf2-mddh1-1"]];
    }
    return _activityImg;
}



@end
